package com.zuowei.ycy.user.ui.order;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.and.yzy.frame.adapter.AdapterCallback;
import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.OrderAdapter;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.OrderInfo;
import com.zuowei.ycy.user.http.Order;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.user.util.AppJsonUtil;
import com.zuowei.ycy.user.util.PtrInitHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/28.
 * 订单列表
 */
public class OrderFgt extends BaseFgt implements AdapterCallback {
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.tv_titile)
    TextView tv_title;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;


    private List<OrderInfo> list;
    private OrderAdapter adapter;
    private OrderInfo item;
    private int p = 1;

    @Override
    public int getLayoutId() {
        return R.layout.order_fgt;
    }

    @Override
    public void initData() {
        tv_title.setBackgroundColor(getResources().getColor(R.color.toolbar_bg));
        //初始化刷新view
        PtrInitHelper.initPtr(getActivity(), ptrFrame);
        ptrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                doHttp(RetrofitUtils.createApi(Order.class).orderList(UserManger.getM_id(), "1"), 0);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listview, header);
            }
        });

        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.loadMoreFinish(false, true);

        loadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(final LoadMoreContainer loadMoreContainer) {
                doHttp(RetrofitUtils.createApi(Order.class).orderList(UserManger.getM_id(), "" + (p + 1)), 1);
            }
        });


        list = new ArrayList<>();
        adapter = new OrderAdapter(getContext(), list, R.layout.mine_order_item, this);
        listview.setAdapter(adapter);
        listview.setEmptyView(llEmpty);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("order_id",adapter.getItem(position).getOrder_id());
                startActivity(OrderDetailsAty.class, bundle);
            }
        });
    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    private boolean isResume;

    @Override
    public void onUserVisible() {
        super.onUserVisible();
        if (UserManger.isLogin()) {
            showLoadingDialog("");
            doHttp(RetrofitUtils.createApi(Order.class).orderList(UserManger.getM_id(), "1"), 0);
        }
    }

    @Override
    public void requestData() {
        if (!UserManger.isLogin()) {
            return;
        }

        showLoadingContentDialog();
        doHttp(RetrofitUtils.createApi(Order.class).orderList(UserManger.getM_id(), "1"), 0);
    }

    @Override
    public void adapterstartActivity(Class<?> className, Bundle options) {

    }

    @Override
    public void adapterstartActivityForResult(Class<?> className, Bundle options, int requestcode) {

    }

    @Override
    public void adapterInfotoActiity(Object data, int what) {
        item = (OrderInfo) data;
        switch (what) {
            case 0: //去支付
                Bundle bundle = new Bundle();
                bundle.putString("order_sn",item.getOrder_sn());
                bundle.putString("order_id",item.getOrder_id());
                bundle.putString("price",item.getOrder_price());
                startActivity(PayOrderAty.class,bundle);
                break;
            case 1: //取消订单
                showLoadingDialog("");
                doHttp(RetrofitUtils.createApi(Order.class).cancelOrder(item.getOrder_id()),2);
                break;
            case 2: //确认收货
                showLoadingDialog("");
                doHttp(RetrofitUtils.createApi(Order.class).confirmOrder(item.getOrder_id()),3);
                break;
            case 3: //评价
                Bundle bundle1 = new Bundle();
                bundle1.putString("order_id",item.getOrder_id());
                startActivity(OrderPinjiaAty.class,bundle1);
                break;
        }

    }

    @Override
    public void onFailure(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what==0){
            isShowToast =false;
            ptrFrame.refreshComplete();
        }else {
            loadMoreListViewContainer.loadMoreFinish(false,false);
        }
        super.onFailure(result, call, response, what);
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        isShowToast =true;
        if (what == 0) {
            list.clear();
            list.addAll(AppJsonUtil.getArrayList(result, OrderInfo.class));
            adapter.notifyDataSetChanged();
            ptrFrame.refreshComplete();
            loadMoreListViewContainer.loadMoreFinish(false,true);
            p=1;
        }else if (what==1){
            list.addAll(AppJsonUtil.getArrayList(result, OrderInfo.class));
            adapter.notifyDataSetChanged();
            ptrFrame.refreshComplete();
            loadMoreListViewContainer.loadMoreFinish(false,true);
            p++;
        }else if (what==2){//取消订单
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            item.setStatus("6");
            adapter.notifyDataSetChanged();
        }else if (what==3){ //确认收货
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            item.setStatus("4");
            adapter.notifyDataSetChanged();
        }
        super.onSuccess(result, call, response, what);
    }
}
